package com.cema.manage.pow;
import org.apache.commons.codec.binary.Hex;

/**
 * 区块
 *
 */

public class Block {

    private static final String ZERO_HASH = Hex.encodeHexString(new byte[32]);

    /**
     * 区块hash值
     */
    private String hash;

    public Block(String s, String previousHash, long currentTimeMillis, int i) {
        this.hash = s;
        this.prevBlockHash = previousHash;
        this.timeStamp = currentTimeMillis;
        this.nonce = i;
    }


    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getHash() {
        return hash;
    }

    /**
     * 前一个区块的hash值
     */
    private String prevBlockHash;

    public void setPrevBlockHash(String prevBlockHash) {
        this.prevBlockHash = prevBlockHash;
    }

    public String getPrevBlockHash() {
        return this.prevBlockHash;
    }

    /**
     * 区块创建时间(单位:秒)
     */
    private long timeStamp;

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    /**
     * 工作量证明计数器
     */
    private long nonce;

    public void setNonce(long nonce) {
        this.nonce = nonce;
    }

    public long getNonce() {
        return this.nonce;
    }

    /**
     * <p> 创建创世区块 </p>
     *
     * @return
     */
    public static Block newGenesisBlock() {
        return Block.newBlock(ZERO_HASH);
    }

    /**
     * <p> 创建新区块 </p>
     *
     * @param previousHash
     * @return
     */
    public static Block newBlock(String previousHash) {
        Block block = new Block("", previousHash, System.currentTimeMillis(), 0);
      //  ProofOfWork pow = ProofOfWork.newProofOfWork(block);
      //  PowResult powResult = pow.run();
        block.setHash("0000000000000000000000000000000000000000000000000000000000000000");
        block.setNonce(0);
        return block;
    }

}
